import * as vscode from "vscode";
import getFilePath from "../vscode/getFilePath";
import { CommandId } from "@ap/command";
/**
 * 根据错误类型显示不同的错误信息
 * @param error 错误对象
 */
export function showErrorMessage(error: any): void {
  // 对错误进行分类处理，展示不同的错误信息
  if (error instanceof Error) {
    vscode.window.showErrorMessage(`发生错误：${error.message}`);
  } else if (typeof error === "string") {
    vscode.window.showErrorMessage(error);
  } else {
    vscode.window.showErrorMessage("发生未知错误");
  }
}

export function showInformationMessage(message: string): void {
  vscode.window.showInformationMessage(message);
}

export function showWarningMessage(message: string): void {
  vscode.window.showWarningMessage(message);
}

export async function showProgress(
  message: string,
  task: (
    progress: vscode.Progress<{ message?: string; increment?: number }>,
  ) => Thenable<any>,
): Promise<void> {
  await vscode.window.withProgress(
    {
      location: vscode.ProgressLocation.Notification,
      title: message,
      cancellable: false,
    },
    task,
  );
}

export function tscErrorMsg(logFile: string): void {
  vscode.window
    .showErrorMessage("tsc编译失败，日志文件: " + logFile, "打开日志", "知道了")
    .then(function (select) {
      if (select === "打开日志") {
        vscode.workspace
          .openTextDocument(vscode.Uri.file(logFile))
          .then((doc) => {
            return vscode.window.showTextDocument(doc);
          });
      }
    });
}

export function webpackErrorMsg(logFile: string): void {
  vscode.window
    .showErrorMessage(
      "webpack打包失败，日志文件: " + logFile,
      "打开日志",
      "知道了",
    )
    .then(function (select) {
      if (select === "打开日志") {
        vscode.workspace
          .openTextDocument(vscode.Uri.file(logFile))
          .then((doc) => {
            return vscode.window.showTextDocument(doc);
          });
      }
    });
}

export async function createrProjectInfoMsg(): Promise<boolean> {
  const select = await vscode.window.showInformationMessage(
    "你确定要新建Arena-Ts项目？当前根目录：" + getFilePath(),
    "新建",
    "取消",
  );
  if (select === "新建") {
    return true;
  }
  return false;
}

export async function openResourcesMsg(): Promise<boolean> {
  const select = await vscode.window.showInformationMessage(
    "同步地图[模型/图片/音频]资源数据结束",
    "查看资源",
    "了解",
  );
  if (select === "查看资源") {
    return true;
  }
  return false;
}

export async function openDiffMsg(msg: string): Promise<boolean> {
  const select = await vscode.window.showInformationMessage(
    msg,
    "查看差异",
    "了解",
  );
  if (select === "查看差异") {
    return true;
  }
  return false;
}

export async function downloadNPMPackageInfoMsg(
  name: string,
  view: string,
): Promise<boolean> {
  const select = await vscode.window.showInformationMessage(
    "你确定要安装npm包吗？（" + name + "）当前根目录：" + getFilePath(),
    "安装",
    "查看详情",
    "取消",
  );
  if (select === "安装") {
    return true;
  } else if (select === "查看详情") {
    vscode.env.openExternal(vscode.Uri.parse(view));
    return false;
  }
  return false;
}

export async function createrProjectNullMsg(): Promise<boolean> {
  const select = await vscode.window.showInformationMessage(
    "你还没有配置新建Ts项目命令，暂无法创建。",
    "配置",
    "取消",
  );
  if (select === "配置") {
    return true;
  }
  return false;
}

export async function deleteMapDataStorageDataNullMsg(
  key: string,
): Promise<boolean> {
  const select = await vscode.window.showInformationMessage(
    "你确定要删除键：" + key + " 的数据吗？",
    "确定",
    "取消",
  );
  if (select === "确定") {
    return true;
  }
  return false;
}

export async function loginNullMsg(errorMsg: string): Promise<boolean> {
  const select = await vscode.window.showInformationMessage(
    errorMsg,
    "立刻登录",
    "取消",
  );
  if (select === "立刻登录") {
    return true;
  }
  return false;
}

export async function mapToolNullMsg(errorMsg: string): Promise<boolean> {
  const select = await vscode.window.showInformationMessage(
    errorMsg,
    "链接地图",
    "取消",
  );
  if (select === "链接地图") {
    vscode.commands.executeCommand(CommandId.file_mapTool);
    return true;
  }
  return false;
}

export async function loginTokenMsg(
  msg: string,
  url: string,
): Promise<boolean> {
  const select = await vscode.window.showInformationMessage(
    msg,
    "打开网址",
    "取消",
  );
  if (select === "打开网址") {
    vscode.env.openExternal(vscode.Uri.parse(url));
    return true;
  }
  return false;
}
